package com.qding.park.manager.service.core.facade.impl.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qding.park.common.core.constant.CacheKeyThree;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.business.manager.adapterfactory.AdapterProxyCreateFail;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.common.core.redis.SerializeUtils;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.core.service.common.LockCarCommonFacade;
import com.qding.park.manager.pojo.TbCarlock;
import com.qding.park.manager.pojo.TbUserInfo;
import com.qding.park.manager.pojo.enums.UserTypeEnum;
import com.qding.park.manager.service.core.biz.LockCarBiz;
import com.qding.park.manager.service.core.mapper.TbCarlockMapper;
import com.qding.park.manager.service.core.mapper.TbUserInfoMapper;

@Service("lockCarCommonFacade")
public class LockCarCommonFacadeImpl implements LockCarCommonFacade {

    @Autowired
    private TbCarlockMapper tbCarlockMapper;

    @Autowired
    private TbUserInfoMapper tbUserInfoMapper;
    
    @Autowired
    private LockCarBiz  lockCarBiz;
    
    /**
     * @Field @jedisClient : TODO(redis client)
     */
    @Autowired
    private JedisClient  jedisClient;
    
    @Override
    public ParkDataResult createLockCarOrder(TbCarlock tbCarlock) throws AdapterProxyCreateFail,ServiceCoreException, AdapterException {
        
        lockCarBiz.createLockCarOrder(tbCarlock);
        
        /***********加入监控列表***************************/
        String hkey=CacheKeyThree.MONITOR_CARLOCK_KEY;
        String value=SerializeUtils.serialize(tbCarlock);
        String key=String.valueOf(tbCarlock.getId());
        jedisClient.hset(hkey, key, value);
        /***********加入监控列表***************************/
        
        return ParkDataResult.createEmpty();
    }

    @Override
    public ParkDataResult unLockCar(String orderId) throws AdapterProxyCreateFail, AdapterException, ServiceCoreException {
        
        lockCarBiz.unLockCar(orderId);
        
        /***********从监控列表删除***************************/
        String hkey=CacheKeyThree.MONITOR_CARLOCK_KEY;
        String key=String.valueOf(orderId);
        jedisClient.hdel(hkey, key);
        /***********从监控列表删除***************************/
        
        return ParkDataResult.createEmpty();    
    }
    
    @Override
    public ParkDataResult lockedCarList(String bjuserid) {

//      TbUserInfo tbUserInfo = tbUserInfoMapper.queryUserByBjUserId(bjuserid);
        TbUserInfo tbUserInfo = tbUserInfoMapper.queryUseByBjuseridType(bjuserid, UserTypeEnum.REGISTER_USER.getType());
        
        int userid=tbUserInfo.getId();
        
        
        List<TbCarlock>  carlocklist=tbCarlockMapper.queryByUserId(userid);
        
        
        List list = new ArrayList();
        
        
        if (!ListUtil.isEmpty(carlocklist)) {
            
            for(TbCarlock lock:carlocklist){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("orderId", lock.getId());
                map.put("parkName", lock.getParkName());
                map.put("carNum", lock.getCarNum());
                map.put("startTime", lock.getStartTime().getTime()/1000);
                map.put("endTime", lock.getEndTime().getTime()/1000);
                list.add(map);
            }
            
            ParkDataResult data = ParkDataResult.createListDate();
            data.putDataOnList(list);
            return data;
        }else{
            return ParkDataResult.createEmpty();
        }
    }

}
